﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sofire.DataComm.Net;
using DataCommTest.AsyncTest;
using DataCommTest.RemotingTest;
using System.Windows.Forms;
using System.Threading;
using System.Xml;
using System.Text.RegularExpressions;
using System.IO;

namespace DataCommTest
{
    class Program
    {
        static void TestAsync()
        {
            AsyncServerTestDemo serverDemo = new AsyncServerTestDemo();
            serverDemo.Test();
            Console.ReadLine();
            AsyncClientTestDemo clientDemo = new AsyncClientTestDemo();
            clientDemo.Test();
        }

        static void TestRemoting()
        {
            RemotingServerTestDemo serverDemo = new RemotingServerTestDemo();
            serverDemo.Test();
            Console.ReadLine();
            RemotingClientTestDemo clientDemo = new RemotingClientTestDemo();
            Test2(clientDemo);
        }

        static void Test2(RemotingClientTestDemo clientDemo)
        {
            clientDemo.Test();
            clientDemo.Test();
            for(int i = 0 ; i < 10 ; i++)
            {
                Test(clientDemo);
            }
        }

        static void Test(RemotingClientTestDemo clientDemo)
        {
            Thread t = new Thread(new ParameterizedThreadStart((o) =>
            {
                for(int i = 0 ; i < 100 ; i++)
                {
                    try
                    {

                        clientDemo.Test();
                    }
                    catch(Exception ex)
                    {
                        Console.WriteLine(ex);
                    }
                }
            }));
            t.IsBackground = true;
            t.Start();
        }

        static void TestData()
        {
            DataCommTest.DataTest.DataTestDemo demo = new DataTest.DataTestDemo();
            demo.Test();
        }

        private readonly static Regex BindRegex = new Regex("#{[^}]+}");
        public class BindItem
        {
            private const string NoneParameterFlag = "none";

            private readonly string _Name;
            public string Name { get { return this._Name; } }

            private readonly string _Type;
            public string Type { get { return this._Type; } }
            public bool IsParameter
            {
                get
                {
                    return this._Type != NoneParameterFlag;
                }
            }

            public BindItem(string bindText)
            {
                var lines = bindText.Split(',');
                this._Name = lines[0].Trim();
                if(lines.Length == 1)
                {
                    this._Name = this._Name.Substring(2, this._Name.Length - 1);
                }
                else
                {
                    this._Name = this._Name.Substring(2);
                    this._Type = lines[1].Trim().RemoveEnd();
                }
            }
        }

        static void PrintGroup(string content, string pattern)
        {
            var match = Regex.Match(content, pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.ExplicitCapture);

            if(match.Success
                && match.Groups.Count == 4 && match.Groups[1].Index == 0)
            {

                var g = match.Groups[2];
                Console.WriteLine(content.Remove(g.Index, g.Length));

            }
            else
            {
                Console.WriteLine(content);
            }
            Console.WriteLine("-----------------");
        }
        static void Main(string[] args)
        {
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            //TestRemoting();
            //TestData();
           // DataCommTest.WebTest.WebTester test = new WebTest.WebTester();
            DataCommTest.XSqlTest.XSqlTester test = new XSqlTest.XSqlTester();
            //DataCommTest.PerstTest.PerstTester test = new PerstTest.PerstTester();
            test.Test();
            //document.Load("XMLFile1.xml");
            //Console.WriteLine(document.ChildNodes.Count);
            Console.ReadLine();
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            Console.WriteLine(e.Exception);
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Console.WriteLine(e.ExceptionObject);
        }
    }
}
